package com.hl.secondHand.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hl.secondHand.common.lang.Result;
import com.hl.secondHand.entity.User;
import com.hl.secondHand.service.IUserService;
import com.hl.secondHand.shiro.AccountProFile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hl
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    MailboxController mailboxController;

    @RequiresAuthentication
    @RequestMapping("/getUser")
    public Result getUser() {
        int id = ((AccountProFile)SecurityUtils.getSubject().getPrincipal()).getId();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", id));
        return Result.succes(user);
    }

    @RequestMapping("/register")
    public Result register(User user) {
        Assert.isNull(userService.getOne(new QueryWrapper<User>().eq("name", user.getName())),"用户名已被注册!请重新输入!");
        user.setPassword(SecureUtil.md5(user.getPassword()));
        userService.save(user);
        return Result.succes(null);
    }

    @RequestMapping("/changePwd")
    public Result changepwd(String name,Long phone,String newPwd) {
        User user = userService.getOne(new QueryWrapper<User>().eq("name", name).eq("phone", phone));
        Assert.notNull(user,"用户名或手机号码错误，请重新输入!");
        user.setPassword(SecureUtil.md5(newPwd));
        user.setLastModifiedDate(LocalDateTime.now());
        userService.saveOrUpdate(user);
        return Result.succes(null);
    }

    @RequiresAuthentication
    @RequestMapping("/reCharge")
    public Result reCharge(float amount) {
        int id = ((AccountProFile)SecurityUtils.getSubject().getPrincipal()).getId();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", id));
        user.setBalance(user.getBalance()+amount);
        user.setLastModifiedDate(LocalDateTime.now());
        userService.saveOrUpdate(user);
        mailboxController.addMail(id,1,"充值成功!当前余额"+user.getBalance()+"元");
        return Result.succes(null);
    }

    @RequiresAuthentication
    @RequestMapping("/edit")
    public Result edit(User user) {
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        Assert.isTrue(user.getId() == nowId, "无权编辑");
        user.setPassword(user.getPassword());
        user.setLastModifiedDate(LocalDateTime.now());
        userService.saveOrUpdate(user);
        mailboxController.addMail(nowId,4,"您的用户信息更改成功!");
        return Result.succes(null);
    }
}
